package com.ruoyi.web.controller.api;

import com.ruoyi.cat.domain.CatChance;
import com.ruoyi.cat.domain.DTO.ArticletaskDTO;
import com.ruoyi.cat.domain.DTO.MyHomeDTO;
import com.ruoyi.cat.domain.DTO.MyShareUserDTO;
import com.ruoyi.cat.domain.DTO.UserActivityDTO;
import com.ruoyi.cat.domain.VO.MyShareVO;
import com.ruoyi.cat.service.ICatActivityTableService;
import com.ruoyi.cat.service.ICatArticletaskTableService;
import com.ruoyi.cat.service.ICatChanceService;
import com.ruoyi.cat.service.ICatShareuserTableService;
import com.ruoyi.cat.service.impl.RichTextService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.web.service.WxTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "api-我的分享相关接口")
@RestController
@RequestMapping("/api/myShare")
@Slf4j
public class MyShareController extends BaseController {

    @Autowired
    private ICatShareuserTableService catShareuserTableService;

    @Autowired
    private RichTextService richTextService;


    @Autowired
    private WxTokenService wxTokenService;

    @Autowired
    private ICatArticletaskTableService catArticletaskTableService;


    @Autowired
    private ICatActivityTableService iCatActivityTableService;

    @Autowired
    private ICatChanceService iCatChanceService;




    @ApiOperation("api-我的-文章分享列表")
    @GetMapping("getArticleList")
    public TableDataInfo getArticleList(HttpServletRequest request, MyShareVO myShareVO) {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        if (wxLoginUser != null) {
            myShareVO.setUserId(wxLoginUser.getUserId());
        }
       // myShareVO.setUserId(122L);
        startPage();
        log.info("文章分享列表 --------------- {}"+myShareVO);
        List<MyShareUserDTO> list = catShareuserTableService.getMyShareUser(myShareVO);
        return getDataTable(list);
    }


    @ApiOperation("api-获取文章富文本页面")
    @GetMapping("getArticle/{id}")
    public ResponseEntity<String> generateVirtualPage(@PathVariable Long id) {
        try {
            //访问哟啊记录用户的ip等信息
            System.out.println(IpUtils.getHostIp());
            String htmlContent = richTextService.generateVirtualPage(id);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_HTML);
            return new ResponseEntity<>(htmlContent, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("页面生成失败");
        }
    }

    @ApiOperation("api-获取文章详情")
    @GetMapping("getArticleDetails")
    public AjaxResult getArticleDetails(Long id) {
        return success(catArticletaskTableService.selectCatArticletaskById(id));
    }


    @ApiOperation("api-我的-活动列表")
    @GetMapping("getActivityList")
    public TableDataInfo getActivityList(HttpServletRequest request,MyShareVO myShareVO) {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        if (wxLoginUser != null) {
            myShareVO.setUserId(wxLoginUser.getUserId());
        }
        log.info("活动列表 --------------- {}"+myShareVO);
       // myShareVO.setActivityState("1");
        startPage();
        List<MyHomeDTO> list = catShareuserTableService.getMyHome(myShareVO);
        return getDataTable(list);
    }


    @ApiOperation("api-获取活动富文本活动介绍页面")
    @GetMapping("getActivity/{id}")
    public ResponseEntity<String> generateVirtualActivity(@PathVariable Long id) {
        try {
            //访问哟啊记录用户的ip等信息
            System.out.println(IpUtils.getHostIp());
            String htmlContent = richTextService.generateVirtualActivity(id);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_HTML);
            return new ResponseEntity<>(htmlContent, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("页面生成失败");
        }
    }

    @ApiOperation("api-获取活动详情")
    @GetMapping("getActivityDetails")
    public AjaxResult getActivityDetails(HttpServletRequest request,Long id,Long userId) {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        if (wxLoginUser== null) {
            wxLoginUser = new WxLoginUser();
            wxLoginUser.setUserId(userId);
        }
        System.out.println("-------777777-----------"+wxLoginUser.getUserId());
        return success(iCatActivityTableService.getActivityTableByMyId(id,wxLoginUser.getUserId()));
    }

    @ApiOperation("api-获取活动抽奖次数,和抽奖列表")
    @GetMapping("getLotteryInfo")
    public AjaxResult getLotteryInfo(HttpServletRequest request,Long id,Long userId) {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        if (wxLoginUser== null) {
            wxLoginUser = new WxLoginUser();
            wxLoginUser.setUserId(userId);
        }
        System.out.println("-------获取活动抽奖次数-----------"+wxLoginUser.getUserId());
        return success(iCatActivityTableService.getLotteryInfo(id,wxLoginUser.getUserId()));
    }

    @ApiOperation("api-活动滚动轮播")
    @GetMapping("getRollingCarousel")
    public AjaxResult getRollingCarousel(HttpServletRequest request,Long articleId) {
        CatChance catChance = new CatChance();
        catChance.setArticleId(articleId);
        return success(iCatChanceService.selectCatChanceList(catChance));
    }






}
